package hxy.bytecode.jdk.cacheline;

/**
 * @author eric
 * https://blog.csdn.net/mofeizhi/article/details/106816026
 */
public class CacheLineTest {
    private static class TClass {
        private volatile long data = 0;
        private volatile long a, b, c, d, e, f, g;  // 测试此行加与不加时运行速度对比
    }

    private static volatile TClass[] tClasses = new TClass[2];

    static {
        tClasses[0] = new TClass();
        tClasses[1] = new TClass();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            for (long i = 0; i < 1000_0000L; i++) {
                tClasses[0].data = i;
            }
        });
        Thread thread2 = new Thread(() -> {
            for (long i = 0; i < 1000_0000L; i++) {
                tClasses[1].data = i;
            }
        });
        final long start = System.nanoTime();
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println((System.nanoTime() - start) / 100_1000);
    }
}
//————————————————
//        版权声明：本文为CSDN博主「Gun+ing」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/weixin_45702700/article/details/114188435